package ruifeng.zcj.dig.ui.shopping;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Base;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.ui.shopping.bean.Category;
import ruifeng.zcj.dig.ui.shopping.bean.TwoCategory;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * Created by Administrator on 2016/9/29 0029.
 */
public class TwoCategoryFragment extends CategoryFragment {
    private static final String CACHE_KEY_PREFIX = "TwoCategoryFragment";

    TwoCategory category = null;
    Category.RowsBean rowsBean = null;
    ShoppingActivity shoppingActivity;


    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);

        shoppingActivity = (ShoppingActivity) mActivity;

        Bundle bundle = getArguments();
        FragmentArgs args = (FragmentArgs) bundle.getSerializable("categoryBundle");
        rowsBean = (Category.RowsBean) args.get("category");


        shoppingActivity.mActivityTopCenter.setText(""+rowsBean.getSuppliertype());

    }

    protected void sendRequestData() {
        OSChinaApi.getsuppliergoodstypelist(categoryType(), supplierid(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    sendRequestDataSucess(responseInfo.result);
                    if (isAdded()) {
                        if (mState == STATE_REFRESH) {
                            onRefreshNetworkSuccess();
                        }
                        Base base = fromJson(responseInfo.result);
                        // 保存数据
                        saveObject(getCacheKey(), base);
                        executeOnLoadDataSuccess(base);// 读取数据成功
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    protected String supplierid() {
        return rowsBean.getSupplierid();
    }

    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter(recyclerView);
    }

    @Override
    protected String getCacheKeyPrefix() {
        if (rowsBean != null) {
            String cacheKey = rowsBean.getSuppliertype().replaceAll("/", "_");
            return CACHE_KEY_PREFIX + rowsBean.getSupplierid() + cacheKey;
        }
        return CACHE_KEY_PREFIX;
    }

    /***
     * 把数据显示到view上面去
     **/
    @Override
    protected void executeOnLoadDataSuccess(Serializable seri) {
        super.executeOnLoadDataSuccess(seri);
        category = (TwoCategory) seri;
        if (mMomentAdapter != null) {
            mMomentAdapter.addNewData(category.getRows());
        }
    }

    @Override
    protected int categoryType() {
        return 1;
    }

    @Override
    protected Base fromJson(String netResult) {
        return (TwoCategory) AbJsonUtil.fromJson(netResult, TwoCategory.class);
    }


    public class MomentAdapter extends BGARecyclerViewAdapter<TwoCategory.RowsBean> {
        public MomentAdapter(RecyclerView recyclerView) {
            super(recyclerView, R.layout.listview_item_category);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, TwoCategory.RowsBean moment) {
           /* if (TextUtils.isEmpty(moment.getPhoto())) {
              //  ImageView imageView = helper.getImageView(R.id.vehicle_fragment_item_img);

            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/
            helper.setVisibility(R.id.vehicle_fragment_item_img, View.GONE);
            // ImageView imageView = helper.getImageView(R.id.vehicle_fragment_item_img);

            helper.setText(R.id.vehicle_fragment_item_title, " " + moment.getSubtiertype());
            helper.setVisibility(R.id.vehicle_fragment_item_address, View.GONE);
          //  helper.setText(R.id.vehicle_fragment_item_address, "齿轮总成/星星齿");

          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }

    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {
        if (category != null) {
            TwoCategory.RowsBean rowsBean = category.getRows().get(position);
            // 发送一个对象给另一个Fragment
            EventBus.getDefault().post(rowsBean);
            shoppingActivity.setCurrentItem(1);
        }

    }

    @Override
    public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
        return false;
    }
}
